package org.s.com.springioc2.controller;

import jakarta.annotation.Resource;
import org.s.com.springioc2.model.UserInfo;
import org.s.com.springioc2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    //@Autowired
    private UserService service;
    @Autowired
    @Qualifier("userInfo2")
    private UserInfo user;
    @Autowired
    @Resource(name = "userInfo1")
    private UserInfo user1;



    //将依赖注入到构造方法中
    //如果我们自己定义了一个有参数的构造方法,即使不用加@Autowired注解也会成功
    //但是我们加上一个无参数的构造方法,程序启动会首先调用无参数的构造方法
    //这样就会造成空指针异常
    //解决办法就是需要在有参数的构造方法下加上@Autowired注解
    public UserController(){

    }
    @Autowired
    public UserController(UserService userService){
        this.service=userService;
    }
    public void say(){
        System.out.println(user);
        System.out.println(user1);
        service.say();
        System.out.println("hi,UserController");
    }
}
